package com.piece.core.mongo.service;

import com.piece.core.log.enums.LoginLogType;
import com.piece.core.mongo.repository.MongoLoginLogRepository;
import com.piece.core.mongo.entity.MongoLoginLog;
import com.piece.core.mongo.support.LoginEchartMap;
import com.piece.core.mongo.support.LoginLogGroup;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.time.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Transactional
@Service(value = "mongoLoginLogService")
public class MongoLoginLogService {

    @Autowired
    private MongoLoginLogRepository mongoLoginLogRepository;

    public MongoLoginLog insert(MongoLoginLog log) {
        return mongoLoginLogRepository.save(log);
    }

    public MongoLoginLog update(MongoLoginLog log) {
        return mongoLoginLogRepository.saveOrUpdate(log.getLogId(), log);
    }

    public void delete(String loginId) {
        MongoLoginLog log = new MongoLoginLog();
        log.setLogId(loginId);
        mongoLoginLogRepository.delete(log);
    }

    public void deleteAll() {
        mongoLoginLogRepository.deleteAll();
    }

    public List<MongoLoginLog> findList(QueryTable queryTable) {
        return mongoLoginLogRepository.find(queryTable);
    }

    public DataTable findPage(QueryTable queryTable) {
        return mongoLoginLogRepository.findPage(queryTable);
    }

    /**
     * 统计本周用户访问量
     *
     * @param length 最近统计天数
     */
    public Map findVisitCharts(int length) {
        Long[] visitors = new Long[length];

        Calendar calendar = Calendar.getInstance();
        Date date_start = DateUtil.addDay(-length + 1);
        calendar.setTime(date_start);
        date_start = DateUtil.setStartDay(calendar).getTime();
        Date date_end = DateUtil.setEndDay(calendar).getTime();
        String[] category = DateUtil.getWeekNameRange(DateUtil.getWeekDay(), -length);

        QueryTable queryTable = new QueryTable();
        queryTable.setStart(1);
        queryTable.setLength(0);
        Map<String, Object> param = new HashMap<>();
        param.put("Q_type_S_EQ", LoginLogType.LOGIN.getType());

        for (int i = 0; i < visitors.length; i++) {
            if (i > 0) {
                date_start = DateUtil.addDay(date_start, 1);
                date_end = DateUtil.addDay(date_end, 1);
            }
            param.put("Q_operaTime_D_GE", date_start);
            param.put("Q_operaTime_D_LE", date_end);
            queryTable.setParams(param);
            visitors[i] = Convert.toLong(mongoLoginLogRepository.count(queryTable), 0l);
        }

        Map response = new HashMap();
        response.put("category", category);
        response.put("visit", visitors);
        return response;
    }

    /**
     * 统计用户访问分布
     */
    public List<LoginEchartMap> findVisitMapCharts(int type) {
        List<LoginEchartMap> results = new ArrayList<>();
        QueryTable queryTable = new QueryTable();
        queryTable.setStart(1);
        queryTable.setLength(0);
        queryTable.setGroup("city");
        Calendar calendar = Calendar.getInstance();
        switch (type) {
            case 0:
                break;
            case 1:
                calendar = DateUtil.getFirstDayOfWeek(calendar);
                break;
            case 2:
                calendar = DateUtil.getFirstDayOfMonth(calendar);
                break;
        }

        Map<String, Object> param = new HashMap<>();
        param.put("Q_type_S_EQ", LoginLogType.LOGIN.getType().toString());
        Date date_start = DateUtil.setStartDay(calendar).getTime();
        param.put("Q_operaTime_D_GE", date_start);
        queryTable.setParams(param);
        List<LoginLogGroup> groups = mongoLoginLogRepository.findByGroup(queryTable, LoginLogGroup.class);

        for (int i = 0; i < groups.size(); i++) {
            results.add(transToEcharts(groups.get(i)));
        }
        return results;
    }

    private LoginEchartMap transToEcharts(LoginLogGroup group) {
        String city = group.getCity();
        try {
            city = city.split("市")[0];
        } catch (Exception e) {
        }
        group.setCity(city);
        return new LoginEchartMap(group.getCity(), group.getCount());
    }
}